home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / generic / putenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-17  |  2.6 KB  |  102 lines

  1. /* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
  2.  
  3. This file is part of the GNU C Library.
  4.  
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with the GNU C Library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include <errno.h>
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
  27. #include <stdlib.h>
  28. #endif
  29. #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
  30. #include <string.h>
  31. #endif
  32. #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
  33. #include <unistd.h>
  34. #endif
  35.  
  36. #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
  37. #define strchr index
  38. #endif
  39. #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
  40. #define memcpy(d,s,n) bcopy ((s), (d), (n))
  41. #endif
  42.  
  43. #ifndef    HAVE_GNU_LD
  44. #define    __environ    environ
  45. #endif
  46.  
  47.  
  48. /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
  49. int
  50. putenv (string)
  51.      const char *string;
  52. {
  53.   const char *const name_end = strchr (string, '=');
  54.   register size_t size;
  55.   register char **ep;
  56.  
  57.   if (name_end == NULL)
  58.     {
  59.       /* Remove the variable from the environment.  */
  60.       size = strlen (string);
  61.       for (ep = __environ; *ep != NULL; ++ep)
  62.     if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
  63.       {
  64.         while (ep[1] != NULL)
  65.           {
  66.         ep[0] = ep[1];
  67.         ++ep;
  68.           }
  69.         *ep = NULL;
  70.         return 0;
  71.       }
  72.     }
  73.  
  74.   size = 0;
  75.   for (ep = __environ; *ep != NULL; ++ep)
  76.     if (!strncmp (*ep, string, name_end - string) &&
  77.     (*ep)[name_end - string] == '=')
  78.       break;
  79.     else
  80.       ++size;
  81.  
  82.   if (*ep == NULL)
  83.     {
  84.       static char **last_environ = NULL;
  85.       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
  86.       if (new_environ == NULL)
  87.     return -1;
  88.       (void) memcpy ((void *) new_environ, (void *) __environ,
  89.              size * sizeof (char *));
  90.       new_environ[size] = (char *) string;
  91.       new_environ[size + 1] = NULL;
  92.       if (last_environ != NULL)
  93.     free ((void *) last_environ);
  94.       last_environ = new_environ;
  95.       __environ = new_environ;
  96.     }
  97.   else
  98.     *ep = (char *) string;
  99.  
  100.   return 0;
  101. }
  102.